<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */
 
class VoucherController extends Zenddn_Controller_Action{
	
    public function init(){
		parent::init();
        parent::loadTemplate('magashop');
	}
	
	public function indexAction(){
	    
	}
    
    public function checkAction(){
    	$this->_helper->viewRenderer->setNoRender();
        $this->_helper->layout()->disableLayout();
        
        $request = $this->_request;
        
        if($request->isXmlHttpRequest()){
            $coupon = $request->getParam('coupon', false);
            
            $model = new Default_Model_Voucher();
            $rs = $model->checkVoucher($coupon);
            $result = 0;
            
            if($rs){
                $ns = new Zend_Session_Namespace('coupon');
                $ns->unlock();
                
                $ns->haveCoupon = true;
                $ns->voucher_code = $rs['voucher_code'];
                $ns->description = $rs['description'];
                $ns->order_value = $rs['order_value'];
                $ns->type = $rs['type'];
                $ns->value = $rs['value'];
                
                $ns->lock();
                $result = 1;
            }
            
            //header('Conten-type: application/json');
            echo $result;
        }
    }
    
    public function removeAction(){
    	$this->_helper->viewRenderer->setNoRender();
        $this->_helper->layout()->disableLayout();
        
        $request = $this->_request;
        
        if($request->isXmlHttpRequest()){
            $ns = new Zend_Session_Namespace('coupon');
            $ns->unlock();
            
            $ns->haveCoupon = false;
            
            $ns->lock();
            
            header('Conten-type: application/json');
            echo json_encode(true);
        }
    }
}